package cn.heycm.blog.console.domain.article.service;

import cn.heycm.blog.console.domain.article.model.AdminArticle;
import cn.heycm.blog.console.domain.article.repository.AdminArticleRepository;
import cn.heycm.blog.console.infra.dao.article.entity.Article;
import cn.heycm.blog.console.infra.dao.article.entity.ArticleHistory;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 文章服务实现
 *
 * @author hey
 * @version 1.0
 * @since 2024/9/26 17:16
 */
@Service
@AllArgsConstructor
public class AdminArticleServiceImpl implements AdminArticleService {

    private final AdminArticleRepository repository;

    @Override
    public AdminArticle findAdminArticle(long articleId) {
        Article article = repository.findById(articleId);
        if (null == article) {
            return null;
        }
        AdminArticle adminArticle = AdminArticle.builder().articleId(articleId).article(article).build();
        this.findHistory(adminArticle);
        return adminArticle;
    }

    @Override
    public void findHistory(AdminArticle adminArticle) {
        List<ArticleHistory> history = repository.findHistory(adminArticle.getArticleId());
        adminArticle.setHistories(history);
    }
}
